package t327.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.File;
import java.util.HashMap;

public class AliOSSUntil {
    // 配置您的阿里云 OSS 访问信息
    private static final String endpoint = "http://oss-cn-shanghai.aliyuncs.com"; // 根据您的实际情况填写正确的 endpoint
    private static final String accessKeyId = "LTAI5tBRnidRkeL5WM6zQR6W";
    private static final String accessKeySecret = "UICQgFjMbztgwoL6DefmrOl6CEZi8X";
    private static final String bucketName = "jskss";

    public static void main(String[] args) {



        // 上传文件
       /* String localFile = "C:\\Users\\27804\\Desktop\\新建文件夹 (3)\\qwe.txt";
        String remoteFile = "qwe.txt";*/


        // 下载文件
       /* String downloadedFile = "<path-to-download-file>";
        downloadFile(ossClient, remoteFile, downloadedFile);*/

        // 关闭 OSS 客户端
        /*ossClient.shutdown();*/
    }
    public static OSS CreatingClient(){
        // 创建 OSS 客户端
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }


    // 上传文件
    public static HashMap uploadFile(OSS ossClient, String localFile, String remoteFile) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, remoteFile, new File(localFile));
            ossClient.putObject(putObjectRequest);
            System.out.println("文件上传成功：" + remoteFile);
            map.put("msg","文件上传成功");
            map.put("code","200");
        } catch (Exception e) {
            System.out.println(e.toString());
            System.out.println("文件上传失败：" + e.getMessage());
            map.put("msg","文件上传失败");
            map.put("code","202");
        }
        return map;
    }

    // 下载文件
    public static void downloadFile(OSS ossClient, String remoteFile, String downloadedFile) {
        try {
            ossClient.getObject(new GetObjectRequest(bucketName, remoteFile), new File(downloadedFile));
            System.out.println("文件下载成功：" + downloadedFile);
        } catch (Exception e) {
            System.out.println("文件下载失败：" + e.getMessage());
        }
    }
}
